<?php

namespace app\admin\service;

use app\common\configs\CommonData;
use app\common\configs\SettingConf;
use app\common\configs\WeChatApi;
use app\common\model\trResource;
use app\common\model\trSystemSet;
use app\common\util\ErrorCode;
use app\common\util\Tools;
use think\Db;
use think\facade\App;

/**
 * Class Auth
 * @package app\admin\service
 * 系统配置服务类
 */
class SystemService
{
    private $returnData;
    private $data = [];
    private $params;
    private $mall_id = 0;

    public function __construct($data, $params)
    {
        $this->returnData = $data;
        $this->params = $params;
        $this->mall_id = request()->mallData["id"];
    }

    // 获取微信小程序配置信息
    public function getWxAppInfo(){
        $wxAppData = trSystemSet::getValue(SettingConf::WX_CHAT_APP_CONFIG);
        foreach (SettingConf::$baseConfigArr[SettingConf::WX_CHAT_APP_CONFIG] as $field => $value) {
            if(!isset($wxAppData[$field])){
                $wxAppData[$field] = $value;
            }
        }
        $data = trSystemSet::getValue(SettingConf::MAP_SETTING);
        foreach (SettingConf::$baseConfigArr[SettingConf::MAP_SETTING] as $field => $value) {
            $wxAppData[$field] = !isset($data[$field]) ? $value : $data[$field];
        }
        $res = Tools::curl(CommonData::WX_MINI_PUBLISH_SERVER_URL.CommonData::$wxMiniPublishRoute['getIp']); // 获取上传小程序服务的IP
        $result = json_decode(trim($res, chr(239) . chr(187) . chr(191)), true);
        $wxAppData['wxUploadIp'] = $result['data']['ip'];
        $this->returnData["data"] = $wxAppData;
        return $this->returnData;
    }

    // 保存微信小程序配置信息
    public function getSaveWxApp(){
        foreach ([SettingConf::WX_CHAT_APP_CONFIG, SettingConf::MAP_SETTING] as $key) {
            $this->data = [];
            foreach (SettingConf::$baseConfigArr[$key] as $field => $value) {
                $this->data[$field] = isset($this->params[$field]) ? trim($this->params[$field]) : $value;
            }
            if(isset($this->data["appId"]) && isset($this->data["appSecret"])){
                $response = Tools::curl(WeChatApi::$weChat_conf["get_token"]."?grant_type=client_credential&appid={$this->data["appId"]}&secret={$this->data["appSecret"]}");
                $result = json_decode($response, true);
                if (isset($result['errcode']) && $result['errcode'] != 0) {
                    return ErrorCode::code("customized", isset(ErrorCode::$wxChatCode[$result["errcode"]]) ? ErrorCode::$wxChatCode[$result["errcode"]] : $result["errmsg"]);
                }
            }
            trSystemSet::toSave($this->data, $key);
        }
        return $this->returnData;
    }

    // 获取短信配置信息
    public function getSmsInfo(){
        foreach (SettingConf::$smsConfigArr as $key => $item){
            $smsData = trSystemSet::getValue($key);
            foreach ($item as $field => $value) {
                $this->data[$field] = !isset($smsData[$field]) ? $value : $smsData[$field];
            }
        }
        $this->data = array_merge($this->data, SettingConf::$smsContent);
        $this->data["payTplStatus"] = intval($this->data["payTplStatus"]);
        $this->data["allocateTplStatus"] = intval($this->data["allocateTplStatus"]);
        $this->data["cancelTplStatus"] = intval($this->data["cancelTplStatus"]);
        $this->data["refundTplStatus"] = intval($this->data["refundTplStatus"]);
        $this->data["acceptTplStatus"] = intval($this->data["acceptTplStatus"]);
        $this->data["remindTplStatus"] = intval($this->data["remindTplStatus"]);
        $this->data["remindAcceptTplStatus"] = intval($this->data["remindAcceptTplStatus"]);
        $this->data["finishTplStatus"] = intval($this->data["finishTplStatus"]);
        $this->returnData["data"] = $this->data;
        return $this->returnData;
    }

    // 保存短信配置信息
    public function getSaveSms(){
        foreach (SettingConf::$smsConfigArr as $key => $item) {
            $this->data = [];
            foreach ($item as $k => $v){
                $this->data[$k] = isset($this->params[$k]) ? trim($this->params[$k]) : $v;
            }
            if(!trSystemSet::toSave($this->data, $key)){
                return ErrorCode::$errorCode["fail"];
            }
        }
        return $this->returnData;
    }

    // 获取提现配置信息
    public function getWithdrawInfo(){
        $withdrawData = trSystemSet::getValue(SettingConf::WITHDRAW_CONFIG);
        foreach (SettingConf::$baseConfigArr[SettingConf::WITHDRAW_CONFIG] as $field => $value) {
            if(!isset($withdrawData[$field])){
                $withdrawData[$field] = $value;
            }
        }
        $withdrawData["withdrawMaxStatus"] = intval($withdrawData["withdrawMaxStatus"]);
        $withdrawData["withdrawMinStatus"] = intval($withdrawData["withdrawMinStatus"]);
        $this->returnData["data"] = $withdrawData;
        return $this->returnData;
    }

    // 保存提现配置信息
    public function getSaveWithdraw(){
        foreach (SettingConf::$baseConfigArr[SettingConf::WITHDRAW_CONFIG] as $k => $v) {
            $this->data[$k] = isset($this->params[$k]) ? trim($this->params[$k]) : $v;
        }
        $result = trSystemSet::toSave($this->data, SettingConf::WITHDRAW_CONFIG);
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    //  获取小程序分享信息
    public function getWxAppQr(){
        $data = trSystemSet::getValue(SettingConf::WX_APP_SHARE);
        foreach (SettingConf::$baseConfigArr[SettingConf::WX_APP_SHARE] as $field => $value) {
            $data[$field] = !empty($data[$field]) ? $data[$field] : $value;
        }
        foreach (SettingConf::$convertArr as $field){
            if(array_key_exists($field, $data)){
                $data[$field."Url"] = '';
                if(!empty($data[$field])){
                    $data[$field."Url"] = tome_dia(trResource::getValue("url", ["id" => $data[$field]]));
                    $data[$field."Url"] = $data[$field."Url"] ?: tome_dia($data[$field]);
                }
            }
        }
        $this->returnData["data"] = $data;
        return $this->returnData;
    }

    //保存分享背景图
    public function getSaveSharePicture(){
        $data = trSystemSet::getValue(SettingConf::WX_APP_SHARE);
        $data['backgroundPicture'] = isset($this->params['picture']) ? $this->params['picture'] : "";
        $result = trSystemSet::toSave($data, SettingConf::WX_APP_SHARE);
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    // 获取专车接送线路备注信息
    public function getCarRemarksInfo(){
        $data = trSystemSet::getValue(SettingConf::CAR_REMARK);
        foreach (SettingConf::$baseConfigArr[SettingConf::CAR_REMARK] as $field => $value) {
            $data[$field] = !empty($data[$field]) ? $data[$field] : $value;
        }
        $this->returnData["data"] = ["remarkContent" => $data["carRemarks"], "alias" => $data["carAlias"]];
        return $this->returnData;
    }

    // 保存专车接送线路备注信息
    public function getSaveCarRemarks(){
        foreach (SettingConf::$baseConfigArr[SettingConf::CAR_REMARK] as $k => $v) {
            $this->data[$k] = isset($this->params[$k]) ? trim($this->params[$k]) : $v;
        }
        $result = trSystemSet::toSave($this->data, SettingConf::CAR_REMARK);
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    // 获取机场线路备注信息
    public function getAirportRemarksInfo(){
        $data = trSystemSet::getValue(SettingConf::AIRPORT_REMARK);
        foreach (SettingConf::$baseConfigArr[SettingConf::AIRPORT_REMARK] as $field => $value) {
            $data[$field] = !empty($data[$field]) ? $data[$field] : $value;
        }
        $this->returnData["data"] = ["remarkContent" => $data["airportRemarks"], "alias" => $data["airportAlias"]];
        return $this->returnData;
    }

    // 保存机场线路备注信息
    public function getSaveAirportRemarks(){
        foreach (SettingConf::$baseConfigArr[SettingConf::AIRPORT_REMARK] as $k => $v) {
            $this->data[$k] = isset($this->params[$k]) ? trim($this->params[$k]) : $v;
        }
        $result = trSystemSet::toSave($this->data, SettingConf::AIRPORT_REMARK);
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    // 获取巴士线路备注信息
    public function getEnterpriseRemarksInfo(){
        $data = trSystemSet::getValue(SettingConf::BUSES_REMARK);
        foreach (SettingConf::$baseConfigArr[SettingConf::BUSES_REMARK] as $field => $value) {
            $data[$field] = !empty($data[$field]) ? $data[$field] : $value;
        }
        foreach (SettingConf::$convertArr as $field){
            if(array_key_exists($field, $data)){
                $data[$field."Url"] = '';
                if(!empty($data[$field])){
                    $data[$field."Url"] = tome_dia(trResource::getValue("url", ["id" => $data[$field]]));
                    $data[$field."Url"] = $data[$field."Url"] ?: tome_dia($data[$field]);
                }
            }
        }
        $this->returnData["data"] = [
            "remarkContent" => $data["enterpriseRemarks"], "alias" => $data["enterpriseAlias"], "sendSmsStatus" => intval($data["sendSmsStatus"]),
            "hotLineBanner" => $data["hotLineBanner"], "hotLineBannerUrl" => tome_dia($data["hotLineBannerUrl"]), "hotLineRemark" => $data["hotLineRemark"],
        ];
        return $this->returnData;
    }

    // 保存巴士线路备注信息
    public function getSaveEnterpriseRemarks(){
        foreach (SettingConf::$baseConfigArr[SettingConf::BUSES_REMARK] as $k => $v) {
            $this->data[$k] = isset($this->params[$k]) ? trim($this->params[$k]) : $v;
        }
        $result = trSystemSet::toSave($this->data, SettingConf::BUSES_REMARK);
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    // 获取包车线路备注信息
    public function getVehicleRemarksInfo(){
        $data = trSystemSet::getValue(SettingConf::VEHICLE_REMARK);
        foreach (SettingConf::$baseConfigArr[SettingConf::VEHICLE_REMARK] as $field => $value) {
            if(!isset($data[$field])){
                $data[$field] = $value;
            }
        }
        foreach (SettingConf::$convertArr as $field){
            if(array_key_exists($field, $data)){
                $data[$field."Url"] = '';
                if(!empty($data[$field])){
                    $data[$field."Url"] = tome_dia(trResource::getValue("url", ["id" => $data[$field]]));
                    $data[$field."Url"] = $data[$field."Url"] ?: tome_dia($data[$field]);
                }
            }
        }
        $this->returnData["data"] = array(
            "remarkContent" => $data["vehicleRemarks"], "alias" => $data["vehicleAlias"], "hotLineBanner" => $data["hotLineBanner"],
            "hotLineBannerUrl" => tome_dia($data["hotLineBannerUrl"]), "hotLineRemark" => $data["hotLineRemark"],
        );
        return $this->returnData;
    }

    // 保存包车线路备注信息
    public function getSaveVehicleRemarks(){
        foreach (SettingConf::$baseConfigArr[SettingConf::VEHICLE_REMARK] as $k => $v) {
            $this->data[$k] = isset($this->params[$k]) ? trim($this->params[$k]) : $v;
        }
        $result = trSystemSet::toSave($this->data, SettingConf::VEHICLE_REMARK);
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    // 获取腾讯云配置
    public function getTc(){
        $this->returnData["data"] = trSystemSet::getValue(SettingConf::TC_CONFIG);
        foreach (SettingConf::$baseConfigArr[SettingConf::TC_CONFIG] as $field => $value) {
            if(!isset($this->returnData["data"][$field])){
                $this->returnData["data"][$field] = $value;
            }
        }
        return $this->returnData;
    }

    // 保存腾讯云配置
    public function getSaveTc(){
        foreach (SettingConf::$baseConfigArr[SettingConf::TC_CONFIG] as $k => $v) {
            $this->data[$k] = isset($this->params[$k]) ? trim($this->params[$k]) : $v;
        }
        $result = trSystemSet::toSave($this->data, SettingConf::TC_CONFIG);
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    // 获取关于我们配置信息
    public function getAboutUsInfo(){
        $data = trSystemSet::getValue(SettingConf::ABOUT_USE_CONF);
        foreach (SettingConf::$baseConfigArr[SettingConf::ABOUT_USE_CONF] as $field => $value) {
            if(!isset($data[$field])){
                $data[$field] = $value;
            }
        }
        $this->returnData["data"] = $data;
        return $this->returnData;
    }

    // 保存关于我们配置信息
    public function getSaveAboutUs(){
        foreach (SettingConf::$baseConfigArr[SettingConf::ABOUT_USE_CONF] as $k => $v) {
            $this->data[$k] = isset($this->params[$k]) ? trim($this->params[$k]) : "";
        }
        $result = trSystemSet::toSave($this->data, SettingConf::ABOUT_USE_CONF);
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    // 获取登录页的配置项
    public function getLoginConfInfo(){
        $data = trSystemSet::getValue(SettingConf::LOGIN_CONFIG);
        foreach (SettingConf::$baseConfigArr[SettingConf::LOGIN_CONFIG] as $field => $value) {
            if(!isset($data[$field])){
                $data[$field] = $value;
            }
        }
        $this->returnData["data"] = $data;
        return $this->returnData;
    }

    // 保存登录页的配置项
    public function getSaveLoginConf(){
        foreach (SettingConf::$baseConfigArr[SettingConf::LOGIN_CONFIG] as $k => $v) {
            $this->data[$k] = isset($this->params[$k]) ? trim($this->params[$k]) : $v;
        }
        $result = trSystemSet::toSave($this->data, SettingConf::LOGIN_CONFIG);
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    // 获取客服的配置项
    public function getCustomerServiceInfo(){
        $data = trSystemSet::getValue(SettingConf::CUSTOMER_SERVICE_CONFIG);
        foreach (SettingConf::$baseConfigArr[SettingConf::CUSTOMER_SERVICE_CONFIG] as $field => $value) {
            if(!isset($data[$field])){
                $data[$field] = $value;
            }
        }
        $this->returnData["data"] = $data;
        return $this->returnData;
    }

    // 保存客服的配置项
    public function getSaveCustomerService(){
        foreach (SettingConf::$baseConfigArr[SettingConf::CUSTOMER_SERVICE_CONFIG] as $k => $v) {
            $this->data[$k] = isset($this->params[$k]) ? trim($this->params[$k]) : "";
        }
        $result = trSystemSet::toSave($this->data, SettingConf::CUSTOMER_SERVICE_CONFIG);
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    // 获取退改规则
    public function getRuleContent(){
        $data = trSystemSet::getValue("ruleContent");
        if(empty($data)){
            $data = [
                "ruleContent" => "",  //规则
            ];
        }
        $ruleData = trSystemSet::getValue("orderCancelFeeRule");
        $data["orderCancelRule"] = (!empty($ruleData) && is_array($ruleData)) ? $ruleData : [];
        $statusData = trSystemSet::getValue("orderRefundVerifyStatus");
        $data["refundVerifyStatus"] = isset($statusData["status"]) ? $statusData["status"] : '1';
        $this->returnData["data"] = $data;
        return $this->returnData;
    }

    // 保存退改规则
    public function getSaveRuleContent(){
        $this->data["ruleContent"] = isset($this->params["ruleContent"]) ? trim($this->params["ruleContent"]) : "";
        $result = trSystemSet::toSave($this->data, "ruleContent");
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        if(!empty($this->params["orderCancelRule"])) {
            $this->data = @json_decode($this->params["orderCancelRule"], true);
            $this->data = $this->data ?: $this->params["orderCancelRule"];
            $result = trSystemSet::toSave($this->data, "orderCancelFeeRule");
            if (!$result) {
                $this->returnData = ErrorCode::$errorCode["fail"];
            }
        }
        $result = trSystemSet::toSave(["status" => isset($this->params["refundVerifyStatus"]) ? $this->params["refundVerifyStatus"] : '1'], "orderRefundVerifyStatus");
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    public function getSiteInfo(){
        $version = Db::query("select version() as ver");
        $data = [
            ["conName" => "程序版本", "conValue" => '租车平台管理系统 v'.CommonData::VERSION],
            ["conName" => "服务器IP地址", "conValue" => request()->ip()],
            ["conName" => "服务器域名", "conValue" => request()->domain()],
            ["conName" => "服务器端口", "conValue" => request()->port()],
            ["conName" => "服务器版本", "conValue" => php_uname('s').php_uname('r')],
            ["conName" => "服务器操作系统", "conValue" => php_uname()],
            ["conName" => "PHP版本", "conValue" => phpversion()],
            ["conName" => "数据库版本", "conValue" => $version[0]['ver']],
            ["conName" => "tp版本", "conValue" => App::version()],
            ["conName" => "服务器当前时间", "conValue" => date("Y-m-d H:i:s")],
            ["conName" => "最大上传限制", "conValue" => get_cfg_var ('upload_max_filesize') ? get_cfg_var ('upload_max_filesize'):'不允许'],
            ["conName" => "最大执行时间", "conValue" => get_cfg_var("max_execution_time")."秒 "],
            ["conName" => "联系我们", "conValue" => "手机/微信：13670093216 QQ：1018608475"],
            ["conName" => "版权所有", "conValue" => "深圳市网商天下科技开发有限公司"],
            ["conName" => "总策划兼项目经理", "conValue" => "车贯 （Michael） 陈元"],
            ["conName" => "研发团队", "conValue" => "陈元  许堉颖  陈镇松  李迪"],
            ["conName" => "设计团队", "conValue" => "林鸿耿"],
            ["conName" => "测试人员", "conValue" => "车贯"],
        ];
        $this->returnData['data'] = $data;
        return $this->returnData;
    }
}